package com.swz.dcrm.ui.viewmodel;

import androidx.lifecycle.MediatorLiveData;

import com.swz.dcrm.api.DcrmApi;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Group;
import com.swz.dcrm.model.GroupGoal;
import com.swz.dcrm.model.Page;
import com.xh.baselibrary.callback.BaseViewModel;
import com.xh.baselibrary.callback.CallBack;

import java.util.List;

import retrofit2.Call;
import retrofit2.Retrofit;

public class GroupViewModel extends BaseViewModel {
    private Retrofit mRetrofit;
    DcrmApi dcrmApi;

    private MediatorLiveData<BaseResponse<List<GroupGoal>>> salesGroupGoal;
    private MediatorLiveData<BaseResponse<GroupGoal>> updateGroupGoal;
    private MediatorLiveData<Group>editGroupResult;
    private MediatorLiveData<BaseResponse<Page<Group>>>groups;
    private MediatorLiveData<BaseResponse<Group>> updateGroup;
    private MediatorLiveData<BaseResponse<Group>>addGroup;
    private MediatorLiveData<BaseResponse<Group>>delGroup;



    public GroupViewModel(Retrofit retrofit){
        this.mRetrofit=retrofit;
        dcrmApi=mRetrofit.create(DcrmApi.class);
    }

    public MediatorLiveData<BaseResponse<List<GroupGoal>>> getSalesGroup(int year, int month) {
        salesGroupGoal=creatLiveData(salesGroupGoal);
        dcrmApi.getStaffGoal(year,month)
                .enqueue(new CallBack<>(this,salesGroupGoal));
        return salesGroupGoal;
    }

    public MediatorLiveData<BaseResponse<Page<Group>>>getGroups(int page,int size){
        groups=creatLiveData(groups);
        dcrmApi.getGroups(page,size).enqueue(new CallBack<>(this,groups));
        return groups;
    }
    public MediatorLiveData<BaseResponse<GroupGoal>> updateGroup(GroupGoal group){
        updateGroupGoal=creatLiveData(updateGroupGoal);
        dcrmApi.updateGroupStaffGoal(group).enqueue(new CallBack<>(this,updateGroupGoal));
        return updateGroupGoal;
    }



    public MediatorLiveData<BaseResponse<Group>> updateGroup(long id,String name) {
        updateGroup=creatLiveData(updateGroup);
        dcrmApi.updateGroup(id,name)
                .enqueue(new CallBack<>(this,updateGroup));
        return updateGroup;
    }

    public MediatorLiveData<BaseResponse<Group>> addGroup(String name) {
        addGroup=creatLiveData(addGroup);
        dcrmApi.addGroup(name)
                .enqueue(new CallBack<>(this,addGroup));
        return addGroup;
    }

    public MediatorLiveData<BaseResponse<Group>> delGroup(long id) {
        delGroup=creatLiveData(delGroup);
        dcrmApi.delGroup(id)
                .enqueue(new CallBack<>(this,delGroup));
        return delGroup;
    }

    public MediatorLiveData<BaseResponse<Group>> getAddGroup() {
        addGroup=creatLiveData(addGroup);
        return addGroup;
    }

    public MediatorLiveData<BaseResponse<Group>> getDelGroup() {
        delGroup=creatLiveData(delGroup);
        return delGroup;
    }

    public MediatorLiveData<Group> getEditGroupResult() {
        editGroupResult=creatLiveData(editGroupResult);
        return editGroupResult;
    }
}
